package com.hc.interceptor;

import com.hc.constant.ProjShow;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 舞遍邓城一条街
 * @description 登录拦截器, 只做session有无登录用户验证
 * @Date 2021/11/24 15:13
 */

@Slf4j
public class IndexInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        log.info("被拦截的请求有：{}", requestURI);
        Object user = request.getSession().getAttribute(ProjShow.SESSION_USER);
        if (ObjectUtils.isEmpty(user)) {
            response.sendRedirect(ProjShow.INDEX_PAGE);
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

}
